package editor.windows;

import java.awt.Color;
import java.text.NumberFormat;

import adventure.AdventureEdit;
import java.awt.Rectangle;
import org.apache.commons.lang.StringUtils;

/**
 *
 * @author Michael Hanns
 *
 */
public class DialogSettingsView extends javax.swing.JDialog {

    private static final long serialVersionUID = 5558805892487742270L;
    private AdventureEdit adv;
    private boolean illegalVersion;
    private boolean illegalLogin;
    private WITNAFrameView parent;
    private Rectangle fullSize;
    private Rectangle loginOff;

    /** Creates new form DialogSettingsView */
    public DialogSettingsView(java.awt.Frame parent, boolean modal, AdventureEdit newAdv) {
        super(parent, modal);
        initComponents();
        adv = newAdv;

        this.parent = (WITNAFrameView) parent;

        String name = adv.getName();
        String version = adv.getVersion() + "";
        String desc = adv.getDescription();
        String author = adv.getAuthor();
        illegalVersion = false;
        illegalLogin = false;

        nameField.setText(name);
        versionField.setText(version);
        descriptionField.setText(desc);
        authorField.setText(author);

        xField.setText(adv.sx() + "");
        yField.setText(adv.sy() + "");
        zField.setText(adv.sz() + "");

        editLoginArea.setSelectionColor(Color.ORANGE);
        editLoginArea.setSelectedTextColor(Color.BLACK);

        fullSize = new Rectangle(this.getBounds().x, this.getBounds().y, this.getBounds().width, this.getBounds().height);
        loginOff = new Rectangle(this.getBounds().x, this.getBounds().y, this.getBounds().width / 2 - 38, this.getBounds().height);

        if (adv.getLoginScreen().length() > 0) {
            customLoginRadio.setSelected(true);
            editLoginArea.setText(adv.getLoginScreen());
        } else {
            defaultLoginRadio.setSelected(true);
        }

        if (defaultLoginRadio.isSelected()) {
            setBounds(loginOff);
        } else {
            setBounds(fullSize);
        }
    }

    private int parseInt(String num) {
        try {
            return Integer.parseInt(num);
        } catch (NumberFormatException e) {
            return 1;
        }
    }

//    private void displayErrorIfIllegalCharacters() {
//        if (!illegalDesc) {
//            descErrorLabel.setForeground(Color.black);
//        }
//        boolean illegal = false;
//        if (descriptionField.getText().contains("<")
//                || descriptionField.getText().contains(">")
//                || descriptionField.getText().contains("&")) {
//            illegal = true;
//        } else if (nameField.getText().contains("<")
//                || nameField.getText().contains(">")
//                || nameField.getText().contains("&")) {
//            illegal = true;
//        } else if (authorField.getText().contains("<")
//                || authorField.getText().contains(">")
//                || authorField.getText().contains("&")) {
//            illegal = true;
//        } else if (editLoginArea.getText().contains("<")
//                || editLoginArea.getText().contains(">")
//                || editLoginArea.getText().contains("&")) {
//            illegal = true;
//        }
//
//        illegalDesc = illegal;
//
//        if (illegalDesc) {
//            descErrorLabel.setText("<html>ERROR: Adventure description, author or login screen must<br/>not contain: <b><>&</b></html>");
//        } else {
//            descErrorLabel.setText(" ");
//        }
//    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jRadioButton1 = new javax.swing.JRadioButton();
        loginScreenGroup = new javax.swing.ButtonGroup();
        applyButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();
        mainPanel = new javax.swing.JPanel();
        comma1 = new javax.swing.JLabel();
        yField = new javax.swing.JFormattedTextField(NumberFormat.getInstance());
        zField = new javax.swing.JFormattedTextField(NumberFormat.getInstance());
        spawnLabel = new javax.swing.JLabel();
        xField = new javax.swing.JFormattedTextField(NumberFormat.getInstance());
        descLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        descriptionField = new javax.swing.JTextArea();
        authorLabel = new javax.swing.JLabel();
        comma2 = new javax.swing.JLabel();
        authorField = new javax.swing.JFormattedTextField();
        defaultLoginRadio = new javax.swing.JRadioButton();
        customLoginRadio = new javax.swing.JRadioButton();
        loginLabel = new javax.swing.JLabel();
        nameField = new javax.swing.JFormattedTextField();
        nameLabel = new javax.swing.JLabel();
        versionErrorLabel = new javax.swing.JLabel();
        versionField = new javax.swing.JFormattedTextField(NumberFormat.getInstance());
        versionSlider = new javax.swing.JSlider();
        versionLabel = new javax.swing.JLabel();
        loginErrorLabel = new javax.swing.JLabel();
        loginPanel = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        editLoginArea = new javax.swing.JTextArea();

        jRadioButton1.setText("jRadioButton1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Edit Settings");
        setAlwaysOnTop(true);
        setResizable(false);

        applyButton.setText("Apply");
        applyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                applyButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        mainPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        comma1.setText(",");

        yField.setToolTipText("Y");
        yField.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                yFieldCaretUpdate(evt);
            }
        });

        zField.setToolTipText("Z");
        zField.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                zFieldCaretUpdate(evt);
            }
        });
        zField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                zFieldActionPerformed(evt);
            }
        });

        spawnLabel.setText("Initial Spawn:");

        xField.setToolTipText("X");
        xField.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                xFieldCaretUpdate(evt);
            }
        });

        descLabel.setText("Description:");

        descriptionField.setColumns(20);
        descriptionField.setFont(new java.awt.Font("Courier New", 0, 12)); // NOI18N
        descriptionField.setLineWrap(true);
        descriptionField.setRows(5);
        descriptionField.setWrapStyleWord(true);
        jScrollPane1.setViewportView(descriptionField);

        authorLabel.setText("Author:");

        comma2.setText(",");

        loginScreenGroup.add(defaultLoginRadio);
        defaultLoginRadio.setSelected(true);
        defaultLoginRadio.setText("Default");
        defaultLoginRadio.setToolTipText("Use the default login screen - this will display your WITNA adventure name and version.");
        defaultLoginRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                defaultLoginRadioActionPerformed(evt);
            }
        });

        loginScreenGroup.add(customLoginRadio);
        customLoginRadio.setText("Custom");
        customLoginRadio.setToolTipText("Use a custom login screen and display any greeting you want up to 80x25 characters at user login.");
        customLoginRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                customLoginRadioActionPerformed(evt);
            }
        });

        loginLabel.setText("Login Screen:");

        nameLabel.setText("Adventure Name:");

        versionField.setText("0");
        versionField.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                versionFieldCaretUpdate(evt);
            }
        });

        versionSlider.setMajorTickSpacing(10);
        versionSlider.setMaximum(200);
        versionSlider.setMinorTickSpacing(1);
        versionSlider.setSnapToTicks(true);
        versionSlider.setValue(0);
        versionSlider.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                versionSliderStateChanged(evt);
            }
        });

        versionLabel.setText("Version:");

        loginErrorLabel.setText(" ");

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGap(64, 64, 64)
                        .addComponent(versionLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(versionField, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(versionSlider, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(authorLabel)
                            .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(nameLabel)
                                .addGroup(mainPanelLayout.createSequentialGroup()
                                    .addGap(18, 18, 18)
                                    .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(descLabel)
                                        .addComponent(loginLabel)))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(loginErrorLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 378, Short.MAX_VALUE)
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addComponent(defaultLoginRadio)
                                .addGap(18, 18, 18)
                                .addComponent(customLoginRadio))
                            .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(versionErrorLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(mainPanelLayout.createSequentialGroup()
                                    .addComponent(spawnLabel)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(xField, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(comma1, javax.swing.GroupLayout.PREFERRED_SIZE, 4, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(yField, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(comma2)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(zField, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 378, Short.MAX_VALUE))
                            .addComponent(authorField, javax.swing.GroupLayout.DEFAULT_SIZE, 378, Short.MAX_VALUE)
                            .addComponent(nameField, javax.swing.GroupLayout.DEFAULT_SIZE, 378, Short.MAX_VALUE))))
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameLabel)
                    .addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(authorField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(authorLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(versionField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(versionLabel))
                    .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(comma1)
                        .addComponent(yField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(comma2)
                        .addComponent(zField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(xField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(spawnLabel))
                    .addComponent(versionSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(versionErrorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(descLabel)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(loginLabel)
                    .addComponent(defaultLoginRadio)
                    .addComponent(customLoginRadio))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(loginErrorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31))
        );

        loginPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        editLoginArea.setBackground(new java.awt.Color(0, 0, 0));
        editLoginArea.setColumns(80);
        editLoginArea.setFont(new java.awt.Font("Courier New", 0, 12)); // NOI18N
        editLoginArea.setForeground(new java.awt.Color(204, 204, 204));
        editLoginArea.setLineWrap(true);
        editLoginArea.setRows(25);
        editLoginArea.setWrapStyleWord(true);
        editLoginArea.setCaretColor(new java.awt.Color(204, 204, 204));
        editLoginArea.setSelectionColor(new java.awt.Color(255, 153, 51));
        editLoginArea.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                editLoginAreaCaretUpdate(evt);
            }
        });
        jScrollPane2.setViewportView(editLoginArea);

        javax.swing.GroupLayout loginPanelLayout = new javax.swing.GroupLayout(loginPanel);
        loginPanel.setLayout(loginPanelLayout);
        loginPanelLayout.setHorizontalGroup(
            loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, loginPanelLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        loginPanelLayout.setVerticalGroup(
            loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, loginPanelLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(applyButton, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(loginPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(loginPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(13, 13, 13)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(applyButton)
                    .addComponent(cancelButton)
                    .addComponent(okButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void versionSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_versionSliderStateChanged
        if (versionSlider.hasFocus()) {
            double version = (double) versionSlider.getValue() / 100;
            versionField.setText("" + version);
            versionErrorLabel.setText("");
        }
    }//GEN-LAST:event_versionSliderStateChanged

    private void versionFieldCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_versionFieldCaretUpdate
        if (!illegalVersion) {
            versionErrorLabel.setForeground(Color.black);
        }
        if (versionField.hasFocus()) {
            int position = -1;
            try {
                position = (int) (Double.parseDouble(versionField.getText()) * 100);
                versionSlider.setValue(position);
                versionErrorLabel.setText("");
                illegalVersion = false;
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }

            if (position < 0 || position > 200) {
                versionErrorLabel.setText("ERROR: Version must be between 0 and 2!");
                illegalVersion = true;
            }
        }
    }//GEN-LAST:event_versionFieldCaretUpdate

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        if (!illegalVersion && !illegalLogin) {
            if (!nameField.getText().equals(adv.getName())) {
                adv.setName(nameField.getText());
                parent.printToConsole("Module name changed to " + nameField.getText() + ".");
            }
            if (Double.parseDouble(versionField.getText()) != adv.getVersion()) {
                adv.setVersion(Double.parseDouble(versionField.getText()));
                parent.printToConsole("Module version updated to " + versionField.getText() + ".");
            }
            if (!authorField.getText().equals(adv.getAuthor())) {
                adv.setAuthor(authorField.getText());
                parent.printToConsole("Module author changed to " + authorField.getText() + ".");
            }
            if (!descriptionField.getText().equals(adv.getDescription())) {
                adv.setDescription(descriptionField.getText());
                parent.printToConsole("Module description updated.");
            }

            if (defaultLoginRadio.isSelected()) {
                adv.setLoginScreen("");
                editLoginArea.setText("");
            } else if (!editLoginArea.getText().equals(adv.getLoginScreen())) {
                adv.setLoginScreen(editLoginArea.getText());
                parent.printToConsole("Module login screen updated.");
            }

            int x = parseInt(xField.getText());
            int y = parseInt(yField.getText());
            int z = parseInt(zField.getText());

            parent.setSpawn(x, y, z);

            boolean aggroMobs = false;
            if (adv.getRoomManager().roomExists(x, y, z)) {
                int mobID = -1;
                for (int c = 0; (mobID = adv.getRoomManager().getMobileIDAtPosition(x, y, z, c)) != 0; c++) {
                    if (adv.getPlaceableManager().getMobile(mobID).isAggro()) {
                        aggroMobs = true;
                    }
                }
                if (aggroMobs) {
                    parent.printToConsole("WARNING: Aggro mobile at spawn position "
                            + "(" + x + ", " + y + ", " + z + "). It is advised that "
                            + "spawn locations remain safe.");
                }
            }

            parent.setUnsavedChanges(true);
            parent.drawFloorToBuffer();
            parent.drawBuffer();

            dispose();
        } else {
            if (illegalVersion) {
                versionErrorLabel.setForeground(Color.red);
            }
            if (illegalLogin) {
                loginErrorLabel.setForeground(Color.red);
            }
        }
    }//GEN-LAST:event_okButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void applyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_applyButtonActionPerformed
        if (!illegalVersion && !illegalLogin) {
            if (!nameField.getText().equals(adv.getName())) {
                adv.setName(nameField.getText());
                parent.printToConsole("Module name changed to " + nameField.getText() + ".");
            }
            if (Double.parseDouble(versionField.getText()) != adv.getVersion()) {
                adv.setVersion(Double.parseDouble(versionField.getText()));
                parent.printToConsole("Module version updated to " + versionField.getText() + ".");
            }
            if (!authorField.getText().equals(adv.getAuthor())) {
                adv.setAuthor(authorField.getText());
                parent.printToConsole("Module author changed to " + authorField.getText() + ".");
            }
            if (!descriptionField.getText().equals(adv.getDescription())) {
                adv.setDescription(descriptionField.getText());
                parent.printToConsole("Module description updated.");
            }
            if (defaultLoginRadio.isSelected()) {
                adv.setLoginScreen("");
                editLoginArea.setText("");
            } else if (!editLoginArea.getText().equals(adv.getLoginScreen())) {
                adv.setLoginScreen(editLoginArea.getText());
                parent.printToConsole("Module login screen updated.");
            }

            int x = parseInt(xField.getText());
            int y = parseInt(yField.getText());
            int z = parseInt(zField.getText());
            adv.setStartArea(x, y, z);

            parent.drawFloorToBuffer();
            parent.drawBuffer();
            parent.setUnsavedChanges(true);

            parent.printToConsole("Spawn location updated to (" + x + ", " + y + ", " + z + ").");

            boolean aggroMobs = false;
            if (adv.getRoomManager().roomExists(x, y, z)) {
                int mobID = -1;
                for (int c = 0; (mobID = adv.getRoomManager().getMobileIDAtPosition(x, y, z, c)) != 0; c++) {
                    if (adv.getPlaceableManager().getMobile(mobID).isAggro()) {
                        aggroMobs = true;
                    }
                }
            }
            if (aggroMobs) {
                parent.printToConsole("WARNING: Aggro mobile at spawn position "
                        + "(" + x + ", " + y + ", " + z + "). It is advised that "
                        + "spawn locations remain safe.");
            }
        } else {
            if (illegalVersion) {
                versionErrorLabel.setForeground(Color.red);
            }
            if (illegalLogin) {
                loginErrorLabel.setForeground(Color.red);
            }
        }
    }//GEN-LAST:event_applyButtonActionPerformed

    private void xFieldCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_xFieldCaretUpdate
    }//GEN-LAST:event_xFieldCaretUpdate

    private void yFieldCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_yFieldCaretUpdate
    }//GEN-LAST:event_yFieldCaretUpdate

    private void zFieldCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_zFieldCaretUpdate
    }//GEN-LAST:event_zFieldCaretUpdate

    private void defaultLoginRadioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_defaultLoginRadioActionPerformed
        if (defaultLoginRadio.isSelected()) {
            setBounds(getBounds().x, getBounds().y, loginOff.width, loginOff.height);
        }
    }//GEN-LAST:event_defaultLoginRadioActionPerformed

    private void customLoginRadioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_customLoginRadioActionPerformed
        if (customLoginRadio.isSelected()) {
            setBounds(getBounds().x, getBounds().y, fullSize.width, fullSize.height);
        }
    }//GEN-LAST:event_customLoginRadioActionPerformed

    private void editLoginAreaCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_editLoginAreaCaretUpdate
        if (customLoginRadio.isSelected()) {
            String[] loginLines = editLoginArea.getText().split("\n");

            boolean illegal = false;
            if (StringUtils.countMatches(editLoginArea.getText(), "\n") > 23) {
                illegal = true;
            }

            for (int x = 0; !illegal && x < loginLines.length; x++) {
                if (loginLines[x].length() > 80) {
                    illegal = true;
                }
            }

            illegalLogin = illegal;

            if (illegalLogin) {
                loginErrorLabel.setText("ERROR: Custom login screen must not exceed 80x24 characters.");
            } else {
                loginErrorLabel.setText(" ");
            }
        }
    }//GEN-LAST:event_editLoginAreaCaretUpdate

    private void zFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_zFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_zFieldActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton applyButton;
    private javax.swing.JFormattedTextField authorField;
    private javax.swing.JLabel authorLabel;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel comma1;
    private javax.swing.JLabel comma2;
    private javax.swing.JRadioButton customLoginRadio;
    private javax.swing.JRadioButton defaultLoginRadio;
    private javax.swing.JLabel descLabel;
    private javax.swing.JTextArea descriptionField;
    private javax.swing.JTextArea editLoginArea;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel loginErrorLabel;
    private javax.swing.JLabel loginLabel;
    private javax.swing.JPanel loginPanel;
    private javax.swing.ButtonGroup loginScreenGroup;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JFormattedTextField nameField;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JButton okButton;
    private javax.swing.JLabel spawnLabel;
    private javax.swing.JLabel versionErrorLabel;
    private javax.swing.JFormattedTextField versionField;
    private javax.swing.JLabel versionLabel;
    private javax.swing.JSlider versionSlider;
    private javax.swing.JFormattedTextField xField;
    private javax.swing.JFormattedTextField yField;
    private javax.swing.JFormattedTextField zField;
    // End of variables declaration//GEN-END:variables
}
